package vn.com.ltv.lo.daos;

import java.util.ArrayList;

import vn.com.ltv.lo.dtos.LevelDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class LevelDAO {
	protected static final String TABLE_NAME = "Level";
	protected static final String ID = "rowid";
	protected static final String LVL_ID = "lvl_id";
	protected static final String LVL_TYPE = "lvl_type";
	protected static String TABLE_CREATION_STRING = "create table "
			+ TABLE_NAME + " (" + ID + " integer primary key autoincrement, "
			+ LVL_ID + " text, " + LVL_TYPE + " text);";
	protected static String TABLE_DROP_STRING = "drop table if exists"
			+ TABLE_NAME + ";";
	private Context context;

	public LevelDAO(Context context) {
		this.context = context;
	}

	public ArrayList<LevelDTO> getAll() {
		ArrayList<LevelDTO> levelDTOs = new ArrayList<LevelDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, null, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				LevelDTO levelDTO = this.cursorToLevelDTO(cursor);
				levelDTOs.add(levelDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return levelDTOs;
	}

	public LevelDTO get(int id) {
		LevelDTO levelDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			levelDTO = this.cursorToLevelDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return levelDTO;
	}

	public void insert(LevelDTO levelDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		ContentValues contentValues = this.levelDTOToContentValues(levelDTO);
		sQLiteDatabase.insert(TABLE_NAME, null, contentValues);
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	/** * @return the number of rows affected */
	public int update(LevelDTO levelDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		int id = levelDTO.getId();
		levelDTO.setId(-1);
		ContentValues contentValues = this.levelDTOToContentValues(levelDTO);
		int rowCount = sQLiteDatabase.update(TABLE_NAME, contentValues, ID
				+ "=?", new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
		return rowCount;
	}

	public void delete(int id) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		sQLiteDatabase.delete(TABLE_NAME, ID + "=?",
				new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	private LevelDTO cursorToLevelDTO(Cursor cursor) {
		LevelDTO levelDTO = new LevelDTO();
		int id = cursor.getInt(cursor.getColumnIndex(ID));
		levelDTO.setId(id);
		String lvlId = cursor.getString(cursor.getColumnIndex(LVL_ID));
		levelDTO.setLvlId(lvlId);
		String lvlType = cursor.getString(cursor.getColumnIndex(LVL_TYPE));
		levelDTO.setLvlType(lvlType);
		return levelDTO;
	}

	private ContentValues levelDTOToContentValues(LevelDTO levelDTO) {
		ContentValues contentValues = new ContentValues();
		int id = levelDTO.getId();
		if (id >= 0) {
			contentValues.put(ID, id);
		}
		String lvlId = levelDTO.getLvlId();
		if (lvlId != null) {
			contentValues.put(LVL_ID, lvlId);
		}
		String lvlType = levelDTO.getLvlType();
		if (lvlType != null) {
			contentValues.put(LVL_TYPE, lvlType);
		}
		return contentValues;
	}
}